package com.lenin.metro.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.metro.shared.MapCoordinates;
import com.lenin.metro.shared.Tile;
import com.lenin.metro.shared.TileGroup;

public class MainPanel extends Composite {

	interface MyUiBinder extends UiBinder<Widget, MainPanel> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	AbsolutePanel headerContainerPanel;

	@UiField
	AbsolutePanel gameFieldContainerPanel;

	private GameFieldPanel gameFieldPanel;

	public MainPanel() {

		initWidget(uiBinder.createAndBindUi(this));

		int windowHeight = Window.getClientHeight();
		int windowWidth = Window.getClientWidth();

		Button testButton1 = new Button("Test 1");
		Button testButton2 = new Button("Test 2");
		Button testButton3 = new Button("Test 3");
		Button testButton4 = new Button("Test 4");
		
		headerContainerPanel.setWidth(windowWidth + "px");
		headerContainerPanel.add(testButton1);
		headerContainerPanel.add(testButton2);
		headerContainerPanel.add(testButton3);
		headerContainerPanel.add(testButton4);
		
		testButton(testButton1, 0);
		testButton(testButton2, 1);
		testButton(testButton3, 2);
		testButton(testButton4, 3);
		
		gameFieldContainerPanel.setWidth(windowWidth + "px");
		gameFieldContainerPanel.setHeight(windowHeight + "px");
		

	}

	
	private void testButton(Button button, final int testId) {
		
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				GreetingServiceAsync service = Metro2033.getService();
				service.test(testId, new AsyncCallback<Void>() {
					public void onSuccess(Void v) {}
					public void onFailure(Throwable caught) {}
				});
			}
		});
		
	}
	
	
	public AbsolutePanel getHeaderContainer() {
		return headerContainerPanel;
	}

	public AbsolutePanel getGameFieldContainer() {
		return gameFieldContainerPanel;
	}

	
	private void initGameField() {
		
		gameFieldPanel = new GameFieldPanel();
		gameFieldPanel.setStyleName("gameFieldPanel");
		gameFieldPanel.getElement().getStyle().setOverflow(Overflow.VISIBLE);
		
		gameFieldContainerPanel.add(gameFieldPanel, 0, 0);
		
		int fieldX = (Window.getClientWidth()-gameFieldPanel.getOffsetWidth())/2;
		gameFieldPanel.getElement().getStyle().setLeft(fieldX, Unit.PX);
		int fieldY = (Window.getClientHeight()-gameFieldPanel.getOffsetHeight())/2;
		gameFieldPanel.getElement().getStyle().setTop(fieldY, Unit.PX);
		
	}
	
	
	@Override
	public void onLoad() {
		
		initGameField();
		
		MapCoordinates centerCoords = new MapCoordinates(0, 0);
		gameFieldPanel.setCenter(centerCoords);
		
		gameFieldPanel.refresh();
		
	}

	
	
}
